import {$http} from '@bana/miniapp-common-util'

module.exports = Behavior({
  externalClasses: ['group-class', 'item-class'],
  /**
   * 组件的属性列表
   */
  properties: {
    dicVal: {
      type: Array,
      value: [],
      observer(newVal, oldVal) {
        console.log('dic behavior dicVal observer ======', newVal, oldVal)
        this._freshStoreDic()
      }
    },
    dicType: {
      type: String,
      value: '',
      observer(newVal, oldVal) {
        console.log('dic behavior dicType observer ======', newVal, oldVal)
        this._freshStoreDic()
      }
    },
    includeOnly: {
      type: Array,
      value: []
    },
    exclude: {
      type: Array,
      value: []
    },
    disabled: {
      type: Boolean,
      value: false
    },
    color: {
      type: String,
      value: '#09BB07'
    },
    rootPath: {
      type: String,
      value: ''
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    items: [],
    pickerVal: null
  },

  attached() {
    this._freshStoreDic()
  },

  /**
   * 组件的方法列表
   */
  methods: {
    change(e) {
      this._triggerChange(e.detail.value)
    },
    pickerChange(e) {
      this.setData({
        pickerVal: e.detail.value
      })
      let dicVal = null
      if (e.detail.value || e.detail.value === 0 || e.detail.value === '0') {
        if (this.data.items[e.detail.value]) {
          dicVal = this.data.items[e.detail.value].businId
        }
      }
      this._triggerChange(dicVal, true)
    },
    _freshStoreDic() {
      const _this = this
      // const dicStore = wx.getStorageSync(_this.data.dicType)
      // if (_this.data.dicType && (dicStore === 'null' || !dicStore)) {
      let dicUrl = 'dictionary/findByType'
      if (_this.properties.rootPath) {
        dicUrl = _this.properties.rootPath + '/dictionary/findByType'
      }
      $http.post({
        url: dicUrl,
        data: {
          busiTypeId: _this.data.dicType,
          delFlag: false
        },
        config: {
          storageCache: true
        },
        success(res) {
          console.log(`${_this.data.dicType} setStorageSync======`, res.data.resultData)
          // wx.setStorageSync(_this.data.dicType, JSON.stringify(res.data.resultData))

          _this._getList(res.data.resultData)
        },
        fail(res) {
          console.log(res)
        }
      })
      // } else {
      //   console.log(`${_this.data.dicType} NO setStorageSync======`)
      //   _this._getList()
      // }
    },
    _getList(dictionaryList = []) {
      const _this = this
      // const dictionaryList = wx.getStorageSync(_this.data.dicType)
      //   ? JSON.parse(wx.getStorageSync(_this.data.dicType)) : []
      console.log('dicVal', _this.data.dicVal, _this.data.includeOnly, _this.data.dicType)
      let tempIndex = null
      dictionaryList.forEach((item) => {
        if (_this.data.dicVal !== null &&
          this.data.dicVal instanceof Array && _this.data.dicVal.length) {
          item.checked = _this.data.dicVal.includes(item.businId)
        } else if (_this.data.dicVal) {
          item.checked = _this.data.dicVal === item.businId
        } else {
          item.checked = false
        }
      })
      if (dictionaryList) {
        const list = dictionaryList
          .filter((item) => (_this.data.includeOnly === null || !_this.data.includeOnly.length) ||
            _this.data.includeOnly.includes(item.businId))
          .filter((item) => (_this.data.exclude === null || !_this.data.exclude.length) ||
            !_this.data.exclude.includes(item.businId))
        list.forEach((item, index) => {
          if (item.checked) {
            tempIndex = index
          }
        })
        _this.setData({
          items: list,
          pickerVal: tempIndex
        })
      } else {
        _this.setData({
          items: dictionaryList,
          pickerVal: null
        })
      }

      // 若传入val存在，则先触发一次change
      if (_this.data.dicVal && _this.data.dicVal.length) {
        _this._triggerChange(_this.data.dicVal, _this.data.isPicker)
      }
    },
    _triggerChange(val, isPicker = false) {
      console.log('isPicker==', isPicker)
      let selObj = null
      const picSelVal = ''
      if (val instanceof Array) {
        selObj = val.length ? this.data.items.filter((item) => val.includes(item.businId)) : []
      } else {
        selObj = val ? this.data.items.find(item => item.businId === val) : ''
      }

      this.triggerEvent('change', {
        selVal: isPicker ? picSelVal : val,
        selObj
      })
    }
  }
})
